﻿/**
* @author Alexander Rindo
* @project Flashview
* @version 0.4
* @purpose Handles individual Thumbnail instances
**/

package com.alexrindo.flashview.UI.thumbnails 
{
	import com.alexrindo.flashview.data.GalleryData;
	import com.alexrindo.flashview.image.ImageManager
	import com.alexrindo.flashview.contextmenu.ThumbContextMenu
	import com.greensock.TweenLite;
	import com.greensock.easing.*;
	import flash.display.Bitmap;
	import flash.display.Loader;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.filters.GlowFilter;
	import flash.net.URLRequest;
	import flash.ui.ContextMenu;
	
	public class Thumbnail extends Sprite
	{
		private const FRAME_WIDTH:int = 4
		private const FRAME_OFFSET:int = 4
		
		private var _urlReq:URLRequest
		private var _count:int
		private var _thumbLoader:Loader
		private var _thumbHolder:Sprite
		private var _images:ImageManager
		private var _data:GalleryData;
		private var _frame:Sprite
		private var _imageWidth:Number
		private var _imageHeight:Number
		private var _glow:GlowFilter
		private var _contextMenu:ThumbContextMenu
		
		public function Thumbnail(url:String, count:int) 
		{
			_count = count
			_urlReq = new URLRequest(url)
			_images = ImageManager.getInstance()
			_data = GalleryData.getInstance()
			
			loadThumbnail(_urlReq)
		}
		
		private function setupContextMenu():void
		{
			if (_data.protectImages == false)
			{
				_contextMenu = new ThumbContextMenu(_count)
				this.contextMenu = _contextMenu.getContextMenu()
			}
		}
		
		public function loadThumbnail(url:URLRequest):void
		{
			_thumbLoader = new Loader()
			_thumbLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, onThumbLoaded)
			_thumbLoader.load(url)
		}
		
		private function onThumbLoaded(e:Event):void 
		{
			dispatchEvent(e)
			
			_thumbHolder = new Sprite()
			_thumbHolder.addChild(new Bitmap(e.target.content.bitmapData))
			_thumbHolder.alpha = 0
			_thumbHolder.filters = [new GlowFilter(0x363636,1)]
			TweenLite.to(_thumbHolder, 0.8, {alpha: 1, ease: Circ.easeOut})
			
			addChild(_thumbHolder)
			addListeners()
			setupContextMenu()
		}
		
		private function addListeners():void
		{
			_thumbHolder.addEventListener(MouseEvent.CLICK, onThumbClick)
			_thumbHolder.addEventListener(MouseEvent.ROLL_OVER, onThumbOver)
			_thumbHolder.addEventListener(MouseEvent.ROLL_OUT, onThumbOut)
			
			useHandCursor = true
			buttonMode = true
		}
		
		private function onThumbOver(e:MouseEvent):void 
		{
			drawFrame()
		}
		
		private function onThumbOut(e:MouseEvent):void 
		{
			clearFrame()
		}
		
		private function onThumbClick(e:MouseEvent):void 
		{
			_images.index = _count
			_images.addImage(_images.index)
		}
		
		private function drawFrame():void
		{
			if (_frame == null) 
			{
				_frame = new Sprite()
				addChild(_frame)
			}
			_frame.graphics.clear()
			_imageWidth = _thumbHolder.width
			_imageHeight = _thumbHolder.height
			_frame.graphics.beginFill(0xFFFFFF, 0.2)
			_frame.graphics.drawRect(-FRAME_OFFSET, -FRAME_OFFSET, _imageWidth + (FRAME_OFFSET*2) - FRAME_WIDTH, FRAME_WIDTH)
			_frame.graphics.drawRect(_imageWidth + FRAME_OFFSET - FRAME_WIDTH, -FRAME_OFFSET, FRAME_WIDTH, _imageHeight + (FRAME_OFFSET*2))
			_frame.graphics.drawRect( -FRAME_OFFSET, _imageHeight + FRAME_OFFSET - FRAME_WIDTH, _imageWidth + (FRAME_OFFSET * 2) - FRAME_WIDTH, FRAME_WIDTH)
			_frame.graphics.drawRect(-FRAME_OFFSET, -FRAME_OFFSET + FRAME_WIDTH, FRAME_WIDTH, _imageHeight + (FRAME_OFFSET*2)- (FRAME_WIDTH*2))
			_frame.graphics.endFill()
		}
		
		private function clearFrame():void
		{
			_frame.graphics.clear()
		}
	}
}